/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Product;
import com.click.app.model.StorageProduct;
import com.click.app.service.GenericManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author orgale
 */
public class StorageProductAction extends BaseAction {

    private GenericManager<StorageProduct, Long> storageProductManager;
    private List storageProducts;
    private StorageProduct storageProduct;
    private Long id;
    private Integer stock;
    private String limit;

    public void setId(Long id) {
        this.id = id;
    }

    public StorageProduct getStorageProduct() {
        return storageProduct;
    }

    public void setStorageProduct(StorageProduct storageProduct) {
        this.storageProduct = storageProduct;
    }

    public String delete() {
        storageProductManager.remove(storageProduct.getId());
        saveMessage(getText("storageProduct.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            storageProduct = storageProductManager.get(id);
            stock = storageProduct.getStock();
            getSession().setAttribute("mat", storageProduct.getProduct());
        } else {
            storageProduct = new StorageProduct();
        }

        return SUCCESS;
    }
    /*
    public boolean validationPasses() {
        Float aux = 0.0f;
        boolean vp = true;
        if (getStock() == null || getStock().matches("")) {
//            errorMessage("Campo requerido, stock.");
            vp = false;
        }
        if (getLimit() == null || getLimit().matches("")) {
//            errorMessage("Campo requerido, limite.");
            vp = false;
        }

        try {
            if (getStock() != null && !getStock().matches("")) {
                aux = Float.parseFloat(getStock());
            }
            if (aux < 0) {
  //              errorMessage("Stock es negativo.");
                vp = false;
            }
        } catch (Exception e) {
//            errorMessage("Stock se encuentra en formato incorrecto.");
            vp = false;
        }

        try {
            if (getLimit() != null && !getLimit().matches("")) {
                aux = Float.parseFloat(getLimit());
            }
            if (aux < 0) {
    //            errorMessage("Limite es negativo.");
                vp = false;
            }
        } catch (Exception e) {
  //          errorMessage("Limite se encuentra en formato incorrecto.");
            vp = false;
        }

        
        return vp;
    }
*/

    public String save() throws Exception {
        storageProduct.setProduct((Product) getSession().getAttribute("mat"));
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (storageProduct.getId() == null);

//        if (validationPasses()){
        Integer s = getStock();
        float l = Float.parseFloat(getLimit());

        storageProduct.setStock(s);

        System.out.println("Stock: " + storageProduct.getStock());

        storageProduct = storageProductManager.save(storageProduct);
      //  }else{
   //         return ERROR;
    //    }
        String key = (isNew) ? "storageProduct.added" : "storageProduct.updated";
        saveMessage(getText(key));
        getSession().removeAttribute("mat");
        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setStorageProductManager(GenericManager<StorageProduct, Long> storageProductManager) {
        this.storageProductManager = storageProductManager;
    }

    public List getStorageProducts() {
        return storageProducts;
    }

    public String list() {
        List<StorageProduct> warning = new ArrayList<StorageProduct>();
        List<StorageProduct> Awarning = new ArrayList<StorageProduct>();
        String smWarning = "";
        String smAWarning = "";
/*
        for (StorageProduct sm : storageProductManager.getAll()) {
            if (sm.getStock() <= sm.getLimit()) {
                warning.add(sm);
                smWarning += " - " + sm.getId();
            } else if ((sm.getStock() <= (sm.getLimit() * .10))) {
                Awarning.add(sm);
                smAWarning += " - " + sm.getId();
            }
        }
 */
 storageProducts = storageProductManager.getAll();
        getRequest().setAttribute("smAllS", storageProductManager.getAll().size());
        getRequest().setAttribute("smAWarningS", Awarning.size());
        getRequest().setAttribute("smWarningS", warning.size());
        getRequest().setAttribute("smWarning", smWarning);
        getRequest().setAttribute("smAWarning", smAWarning);


        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }

    /**
     * @return the stock
     */
    public Integer getStock() {
        return stock;
    }

    /**
     * @param stock the stock to set
     */
    public void setStock(Integer stock) {
        this.stock = stock;
    }

    /**
     * @return the limit
     */
    public String getLimit() {
        return limit;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(String limit) {
        this.limit = limit;
    }
}